//
//  ByBottomPopAnimator.m
//  Private_Object
//
//  Created by pro on 16/9/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByBottomPopAnimator.h"

@implementation ByBottomPopAnimator
{
    UIView *_fromTempView;
    UIControl *_maskView;
}
- (void)xw_setToAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 先让需要的控制器的视图到最底部去
    toVC.view.frame = CGRectOffset(containerView.bounds, 0, containerView.bounds.size.height);
    // 复制一份fromvc.view的视图，用于显示，并设置为全局，方便back的时候使用
    _fromTempView = [fromVC.view snapshotViewAfterScreenUpdates:NO];
    _fromTempView.frame = containerView.bounds;
    _fromTempView.layer.zPosition = -1000;  // 因为旋转的中心为(0.5,0.5)，所以先让z轴往里1000，否则效果不对

    _maskView = [[UIControl alloc] init];
    _maskView.frame = _fromTempView.frame;
    _maskView.backgroundColor = [UIColor blackColor];
    _maskView.alpha = 0;
    
    [_maskView addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    [containerView addSubview:toVC.view];
    [containerView insertSubview:_fromTempView belowSubview:toVC.view];
    [containerView insertSubview:_maskView aboveSubview:_fromTempView];
    [containerView bringSubviewToFront:toVC.view];

    fromVC.view.hidden = YES;
  
    [UIView animateKeyframesWithDuration:self.toDuration delay:0 options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
            _fromTempView.layer.transform = [self stepOneTransform];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
            _fromTempView.layer.transform = [self stepTwoTransformWithView:_fromTempView];
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:1.0 animations:^{
            _maskView.alpha = 0.4;
            toVC.view.frame = CGRectOffset(containerView.bounds, 0, 200);
        }];
        
    } completion:^(BOOL finished) {
        
        
        if ([transitionContext transitionWasCancelled]) {
            fromVC.view.hidden = NO;
            [_fromTempView removeFromSuperview];
        }
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    
    
    
}

- (void)back {
    if (self.backConfig) {
        self.backConfig();
    } else {
        NSLog(@"请设置返回回调，否则不能点击返回");
    }
}

- (void)xw_setBackAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [containerView insertSubview:toVC.view atIndex:0];
    toVC.view.hidden = YES;
    
    [UIView animateKeyframesWithDuration:self.backDuration delay:0 options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
            _fromTempView.layer.transform = [self stepOneTransform];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
            _fromTempView.layer.transform = CATransform3DIdentity;
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 animations:^{
            fromVC.view.frame = CGRectOffset(containerView.bounds, 0, containerView.height);
            _maskView.alpha = 0;
        }];
    } completion:^(BOOL finished) {
        if (![transitionContext transitionWasCancelled]) {
            toVC.view.hidden = NO;
            [_fromTempView removeFromSuperview];
            [_maskView removeFromSuperview];
        }
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        
        
    }];
    
    
}


- (CATransform3D)stepOneTransform {
    CGFloat rotationRatio = 1;
    CATransform3D t1 = CATransform3DIdentity;
    t1.m34 = 1.0 / -900;
    t1 = CATransform3DScale(t1, 0.95, 0.95, 1);
    t1 = CATransform3DRotate(t1, 15.0f * M_PI / 180.0f * rotationRatio, 1, 0, 0);
    return t1;
}

- (CATransform3D)stepTwoTransformWithView:(UIView*)view {
    CATransform3D t2 = CATransform3DIdentity;
    t2.m34 = [self stepOneTransform].m34;
    CGFloat x = 0;
    CGFloat y = view.frame.size.height * -0.08;
    t2 = CATransform3DTranslate(t2, x, y, 0);
    t2 = CATransform3DScale(t2, 0.8, 0.8, 1);
    return t2;
}
@end
